﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DBSchemaViewer.BackEnd.Entities;
using DBSchemaViewer.Business.Controller.Messages;
using DBSchemaViewer.Business.Controller.IViews;
using DBSchemaViewer.FrontEnd.DiagramUserControls;


namespace DBSchemaViewer.FrontEnd.Main
{
    /// <summary>
    /// Here we are going to hold all the IViewMainPage implementation.
    /// 
    /// By using this partial class, code will get more easy to understand and follow.
    /// </summary>
    public partial class Page : UserControl, IViewMainPage
    {
        /// <summary>
        /// This one will be called before creating or loading a new document.
        /// Remove all the controls by UI ID
        /// </summary>
        public void WipeAllTheDiagramUIControls()
        {
            // 1. Remove the list of dynamic controls.
            // 2. Remove the dynamic user controls created so far.

            _dynamicCOntrolsListHelper.wipeAllDynamicControls();
            ctlDiagramView.WipeChildControls();
        }


        /// <summary>
        /// Just creates the UI control for a given relation
        /// </summary>
        /// <param name="newRelation"></param>
        public void CreateRelationControl(EntityRelation newRelation)
        {
            CtlDBRelationship currRelationship = new CtlDBRelationship();

            ctlDiagramView.LayoutRoot.Children.Add(currRelationship);
            currRelationship.EntityRelation_info = newRelation;
            // Relation ZOrder = 2 below tables
            currRelationship.SetValue(Canvas.ZIndexProperty, 2);

            _relationshipDraggingHelper.SetPointOriginAndDestinationFromGivenRelationshipEntity(_dynamicCOntrolsListHelper, currRelationship);

            currRelationship.LButtonDownBubbled += new CtlDBRelationship.LButtonDownBubblingEventHandler(RelationshipControl_LButtonDownBubbled);

            _dynamicCOntrolsListHelper.AddRelationshipControl(newRelation.InternalID, currRelationship);
        }

        public void RefreshMainCanvas()
        {
            ctlDiagramView.UpdateLayout();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="newSize"></param>
        public void UpdateCanvasSize(Size newSize)
        {
            ctlDiagramView.LayoutRoot.Width = newSize.Width;
            ctlDiagramView.LayoutRoot.Height = newSize.Height;
        }

        /// <summary>
        /// Execute this after loading a new document, or once we remove a given element
        /// </summary>
        public void ResetSelectedControl()
        {
            _currSelectedControl = null;
        }


        /// <summary>
        /// Forces a refresh in an existing relation in order to update the canvas position of relation
        /// (e.g. recalcualte origina and destination because on of the related tables has been moved by 
        /// the user).
        /// </summary>
        /// <param name="relation"></param>
        public void UpdateRelationControlCanvasPosition(EntityRelation relation)
        {
            CtlDBRelationship currRelationship = _dynamicCOntrolsListHelper.GetRelationshipControlByID(relation.InternalID);

            _relationshipDraggingHelper.SetPointOriginAndDestinationFromGivenRelationshipEntity(_dynamicCOntrolsListHelper, currRelationship);
        }

        /// <summary>
        /// Just when the entity change but there's no need to update any user interface
        /// on a table, e.g.: when you remove a relation, you just remove the FK from the 
        /// table entity but this column is not shown at all in the UI, no need to refresh
        /// just only update the entity on the control.
        /// </summary>
        /// <param name="modifiedTable"></param>
        public void UpdateTableEntityOnControlButNotUI(EntityTable modifiedTable)
        {
            CtlDBTable currTable = _dynamicCOntrolsListHelper.GetTableControlByID(modifiedTable.InternalID);

            if (currTable != null)
            {
                currTable.EntityTable_info = modifiedTable;
            }
        }


        /// <summary>
        /// 1. Find the control in the layout and remove it form the canvas
        /// 2. Remove the control from the layout
        /// 3. Remove the controls from the list helper
        /// </summary>
        /// <param name="relationInternalID"></param>
        public void RemoveRelationControl(int relationInternalID)
        {
            // 1. Find the control in the layout and remove it form the canvas
            CtlDBRelationship CtlrelationToRemove = _dynamicCOntrolsListHelper.GetRelationshipControlByID(relationInternalID);

            // 2. Remove the control from the layout
            ctlDiagramView.LayoutRoot.Children.Remove(CtlrelationToRemove);


            // 3. Remove the controls from the list helper
            _dynamicCOntrolsListHelper.removeRelationshipControl(relationInternalID);
        }

        /// <summary>
        /// Creates a table control based on a given entityTable, steps:
        /// 
        /// 1. Create and display the control.
        /// 2. Add it to the list of dynamic table controls created
        /// 
        /// This method should be called by the controller, once it
        /// has add it to the XML back end.
        /// </summary>
        /// <param name="table"></param>
        public void CreateTableControl(EntityTable newTable)
        {
            CtlDBTable currTableControl = new CtlDBTable();

            ctlDiagramView.LayoutRoot.Children.Add(currTableControl);
            // TODO: Issue here with position (hardcoded) and not propagating as well to the data model??
            // position  should be set before and this methoud should have it informed as a param
            currTableControl.SetValue(Canvas.LeftProperty, newTable.CanvasCoordinates.X);
            currTableControl.SetValue(Canvas.TopProperty, newTable.CanvasCoordinates.Y);
            // Relation ZOrder = 2 below tables
            currTableControl.SetValue(Canvas.ZIndexProperty, 3);
            currTableControl.EntityTable_info = newTable;

            currTableControl.LButtonDownBubbled += new CtlDBTable.LButtonDownBubblingEventHandler(TableControl_LButtonDownBubbled);
            //currTableControl.LButtonUpBubbled += new CtlDBTable.LButtonUpBubblingEventHandler(TableControl_LButtonUpBubbled);

            _dynamicCOntrolsListHelper.AddTableControl(newTable.InternalID, currTableControl);
        }


    }
}
